#include<iostream>
#include<vector>
#include<unordered_map>
using namespace std;

struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

unordered_map<int,int> key;


TreeNode* build_tree(vector<int>& preorder,vector<int>&inorder,int pl,int pr,int il,int ir)
{
    if(pl>pr)
    {
        return nullptr;
    }
    int n=key[preorder[pl]];
    TreeNode* root=new TreeNode(preorder[pl]);
    int size=n-il;      //计算个数
    root->left=build_tree(preorder,inorder,pl+1,pl+size,il,n-1);
    root->right=build_tree(preorder,inorder,pl+size+1,pr,n+1,ir);
    return root;
}

TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) 
{
    for(int i=0;i<inorder.size();i++)
    {
        key[inorder[i]]=i;
    }
    int n=preorder.size();
    return build_tree(preorder,inorder,0,n-1,0,n-1);

}

int main()
{
    vector<int> preorder;
    vector<int> inorder;
    int cnt=5;
    int num=0;
    for(int i=0;i<cnt;i++)
    {
        cin>>num;
        preorder.push_back(num);
    }
    for (int j = 0; j < cnt; j++)
    {
        cin >> num;
        inorder.push_back(num);
    }
     buildTree(preorder,inorder);

    return 0;
}